<?php

namespace maotool\core\text\password\rule;

use maotool\core\text\password\PasswdStrengthRule;
use maotool\core\text\password\result\PasswdStrengthError;
use maotool\core\text\password\result\PasswdStrengthIgnore;
use maotool\core\text\password\result\PasswdStrengthSuccess;

class WeakRule implements PasswdStrengthRule
{

    protected $hazy;

    protected $message;

    protected $list = [
        "password", "abc123", "iloveyou", "adobe123", "123123", "sunshine",
        "1314520", "a1b2c3", "123qwe", "aaa111", "qweasd", "admin", "passwd",
        "123456", "12345", "password", "123456789", "12345678", 'qwerty',
        'letmein', 'welcome', 'monkey', '123123', '111', '222', '333', '444',
        '555', '666', '777', '888', '999', '000'
    ];

    public function __construct($hazy = false, $message = "密码不能包含常用弱口令", $list = null)
    {
        $this->hazy = $hazy;
        $this->message = $message;
        if (!is_null($list)) {
            $this->list = $list;
        }
    }

    function handle($password)
    {
        if ($this->hazy) {
            foreach ($this->list as $item) {
                if (strstr($password, $item) !== false) {
                    $passwdStrengthError = new PasswdStrengthError();
                    $passwdStrengthError->setMessage($this->message);
                    $passwdStrengthError->setName("weak");
                    return $passwdStrengthError;
                }
            }
        } else {
            if (in_array($password, $this->list)) {
                $passwdStrengthError = new PasswdStrengthError();
                $passwdStrengthError->setMessage($this->message);
                $passwdStrengthError->setName("weak");
                return $passwdStrengthError;
            }
        }
        $passwdStrengthIgnore = new PasswdStrengthIgnore();
        $passwdStrengthIgnore->setName("weak");
        return $passwdStrengthIgnore;
    }

}